package palms.messages;

import java.io.IOException;
import java.net.Socket;

import javax.crypto.SealedObject;

import palms.exceptions.InvalidClassMessage;
import palms.exceptions.InvalidTicketMessage;
import palms.exceptions.InvalidUserMessage;
import palms.security.PalmsSecurityManager;

public class TGSRequest extends PMessage {

	
	private static final long serialVersionUID = -4279273290507581351L;
	
	private SealedObject tgsTicket;
	private SealedObject clientInfo;
	
	public TGSRequest(SealedObject tgsTicket, SealedObject clientInfo) {
		this.tgsTicket = tgsTicket;
		this.clientInfo = clientInfo;
	}
	public TGSRequest() {
	}
	public SealedObject getTgsTicket() {
		return tgsTicket;
	}
	public void setTgsTicket(SealedObject tgsTicket) {
		this.tgsTicket = tgsTicket;
	}
	public SealedObject getClientInfo() {
		return clientInfo;
	}
	public void setClientInfo(SealedObject clientInfo) {
		this.clientInfo = clientInfo;
	}

	public void sendTroughSocket(Socket s) throws IOException{
		PalmsSecurityManager.sendObjectTroughtSocket(s, new TGSRequest());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getTgsTicket());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getClientInfo());
	}
	
	public static TGSRequest receiveTroughSocket(Socket s) throws IOException, ClassNotFoundException, InvalidClassMessage, InvalidUserMessage, InvalidTicketMessage{
		Object o = PalmsSecurityManager.receiveObjectTroughSocket(s);
		if(!(o instanceof TGSRequest))
			PalmsSecurityManager.throwCommException(o);
		else{
			TGSRequest tr = (TGSRequest)o;
			SealedObject ticket = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			SealedObject cinfo = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			tr.setTgsTicket(ticket);
			tr.setClientInfo(cinfo);
			return tr;
		}
		return null;
	}
	
}
